// Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
// Distributable under LGPL license. See terms of license at gnu.org.

package nl.justobjects.pushlet.test;

import java.applet.Applet;
import java.awt.TextArea;
import java.awt.GridLayout;
import java.awt.Color;
import java.io.*;
import java.net.*;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.client.JavaPushletClient;
import nl.justobjects.pushlet.client.JavaPushletClientListener;
// import netscape.javascript.*; not yet!!!
                         
/**
 * Placeholder for future pullet applet.
 *
 * @version $Id: PulletApplet.java,v 1.3 2003/08/15 08:37:41 justb Exp $
 * @author Just van den Broecke - Just Objects &copy;
 **/
public class PulletApplet extends Applet implements Runnable {
  	private TextArea textArea=new TextArea(15, 40);
    private String url=null;
	private boolean running = false;
	
    public PulletApplet() {
    }

	/** Setup AWT stuff. */
	public void init() {
		dbg("init");
		String host = getDocumentBase().getHost();
		int port = getDocumentBase().getPort();
		url = "http://" + host + ":" + port + "/index.html";

	    setLayout(new GridLayout(1,1));
	    textArea.setForeground(Color.yellow);
	    textArea.setBackground(Color.gray);
	  	textArea.setEditable(false);
	  	add(textArea);
    }

	public void doJS(String s)  {
    /*      JSObject win   = JSObject.getWindow(this);                // get handle to a window.
          JSObject jsVar = (JSObject) win.getMember("myObject");    // access  "myObject" as a member of the window object.
          String theValue = (String) jsVar.getMember("mydata");     // access "mydata" as a member of "myObject".
          
          //System.err.println("The script variable = " + theValue);  // prints "hello world".
      
          win.eval("myObject.doIt('" + s + "')");         // one way to set "mydata" from Java.
          System.err.println("OK called JS");  // prints "hello world".
                                                                    // win.eval() works fine for *setting* values.
                                                                    
          //jsVar.setMember("data","Henry VIII");                     // a second way to set "mydata".
          
          //jsVar =  (JSObject) win.getMember("myOjbect");            // re-fetch the values.
          //theValue = (String) jsVar.getMember("mydata"); 
          //System.err.println("The revised script variable = " + theValue);   // should print "Henry VIII". 
	*/
	}
	
	public void start() {
		dbg("start");
		new Thread(this).start();
		running = true;
	}

    public void run() {
		int i=0;
		while (running)  {
			doJS("<pre>   ok-"+(i++)+     "</pre>");
			try  {
				Thread.sleep(3000);
			}catch (InterruptedException ie)  {
			}
		}
    }
	
    public void stop() {
		dbg("stop");
		running = false;
    }

	private String URL2String(String urlString) {
		BufferedReader br = null;
		InputStream is = null;
		String nextLine = "";
		String result = "";
		
		// Read line from server
		try {
			is = new URL(urlString).openStream ();
			br = new BufferedReader(new InputStreamReader(is));
			boolean foundLine = false;
			while (nextLine != null) {
				nextLine = br.readLine();
				if (nextLine != null)  {
					result += nextLine;
				}
			}
		} catch (Exception e) {
			error("could not open or read URL urlString="+urlString+" ex="+e);
		} finally {
			try { 
				if (is != null) is.close(); 
			} catch (IOException ignore) {
			}
		}
		return result;
	}
		
    /** Error occurred. */
    public void error(String message) {
    	p(message);
    }

    /** Generic print. */
	public void p(String s) {
		dbg("event: "+s);
		synchronized (textArea) {
			textArea.append(s+"\n");
		}
	}

   /** Generic print. */
	public void dbg(String s) {
		System.out.println("PushletApplet: " + s);
	}
}

 /*
  * $Log: PulletApplet.java,v $
  * Revision 1.3  2003/08/15 08:37:41  justb
  * fix/add Copyright+LGPL file headers and footers
  *
  * Revision 1.2  2003/05/18 16:15:08  justb
  * support for XML encoded Events
  *
  * Revision 1.1.1.1  2002/09/24 21:02:32  justb
  * import to sourceforge
  *
  * Revision 1.1.1.1  2002/09/20 22:48:19  justb
  * import to SF
  *
  * Revision 1.1.1.1  2002/09/20 14:19:02  justb
  * first import into SF
  *
  * Revision 1.2  2002/04/15 20:42:41  just
  * reformatting and renaming GuardedQueue to BlockingQueue
  *
  * Revision 1.1  2000/10/30 14:17:14  just
  * no message
  *
  * Revision 1.3  2000/08/31 12:48:54  just
  * URI through parameter and changed display of events
  *
  *
  */
  
